import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../model/ImagePreviewViewModel.dart';
import 'ImageScrollWidget.dart';

class ImageWidget extends StatefulWidget {
  const ImageWidget({super.key});

  @override
  _ImageWidgetState createState() => _ImageWidgetState();
}

class _ImageWidgetState extends State<ImageWidget>
    with AutomaticKeepAliveClientMixin {
  final ImagePreviewViewModel viewModel = ImagePreviewViewModel();

  @override
  void initState() {
    super.initState();
    viewModel.initData();
    if (kDebugMode) {
      print("ImagePreview: ImagePreviewWidget initState开始.");
    }
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => viewModel,
      child: Scaffold(
          appBar: AppBar(
            title: const Text('左右滑动图片预览'),
          ),
          body: Consumer<ImagePreviewViewModel>(
            builder: (context, viewModel, child) {
              return ImageScrollWidget(imageData: viewModel.imageData);
            },
          )),
    );
  }

  Widget buildError(String error) {
    return Container(
      color: Colors.black, // 深色背景
      padding: const EdgeInsets.all(16),
      child: Center(
        child: Text(
          error,
          style: const TextStyle(
            color: Colors.white,
            fontSize: 16,
          ),
        ),
      ),
    );
  }

  Widget buildLoading() {
    return Container(
      color: Colors.black, // 深色背景
      child: const Center(
        child: CircularProgressIndicator(
          value: 1,
          valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
